from django.shortcuts import render, redirect
from django.http import Http404
from . import models
from django.conf import settings
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
# from apps.blog.models import Article

# Create your views here..
def home(request, page_no=1):
    """  首页 文章列表概括 """
    page_num = settings.PAGE_NUM
    context = {}

    # 第一页 [0:2] ，第2页 [2, 2]
    _limit_start = (page_no-1) * page_num
    _limit_end = page_no * page_num
    # select * from blog_article order by pub_time dasc limit 0,2;   # sql limit 开始索引，向后取几个
    article_list = models.Article.objects.all().order_by('-pub_time')[_limit_start:page_num]  # [开始索引,结束索引]


    context['article_list'] = article_list
    context['page_no'] = page_no
    return render(request, 'home.html', context=context)

def detail(request, article_id):
    print(id)
    # reversed('detail', kwargs={'article_id':3})
